<script>
	import {
		mapState,
		mapGetters,
		mapActions,
		mapMutations
	} from 'vuex'
	import {
		logOut
	} from "./js_sdk/uni-admin/user.js"
	import config from '@/admin.config.js'
	import {
		version
	} from './package.json'
	import {
		getCoinList
	} from "./js_sdk/uni-admin/wallet.js"
	const timeLimit = 15 * 60 * 1000
	export default {
		computed: {
			...mapGetters({
				isTokenValid: 'user/isTokenValid'
			}),
			...mapState("user", ["token"])
		},
		watch: {
			token(newVal) {
				if (newVal) {
					this.setupTimer()
				}
			}
		},
		methods: {
			...mapActions({
				init: 'app/init'
			}),
			...mapMutations("user", ["REMOVE_TOKEN"]),
			clearTimer() {
				if (this.timeId) {
					window.clearInterval(this.timeId)
					this.timeId = null
				}
			},
			setupTimer() {
				if (!this.isTokenValid) {
					return
				}
				if (!this.token) {
					return
				}
				this.clearTimer()
				this.timeId = setInterval(() => {
					if (this.time) {
						this.checkTimeout()
					}
				}, 1000)
			},
			checkTimeout() {
				let elapsed = Date.now() - this.time
				// console.log(`用户未操作已达${elapsed}毫秒`)
				if (elapsed > timeLimit) {
					console.log('超时，退出登陆')
					this.clearTimer()
					logOut()
					this.REMOVE_TOKEN()
					uni.reLaunch({
						url: '/pages/index/statisics'
					})
				}
			},
			setup() {
				// console.log('setup')
				document.body.addEventListener('click', () => {
					this.time = Date.now()
					uni.setStorageSync('time', this.time)
					// console.log('document click')
				})
				document.body.addEventListener('onmousemove', () => {
					// console.log('document onmousemove')
					this.time = Date.now()
					uni.setStorageSync('time', this.time)
				})
			}
		},
		onPageNotFound(msg) {
			uni.redirectTo({
				url: config.error.url
			})
		},
		onLaunch() {
			// console.log('App Launch')
			this.time = uni.getStorageSync('time') || Date.now()
			this.$nextTick(() => {
				this.setup()
			})
		},
		async onShow() {
			// console.log('App Show')
			// await getCoinList().then(res => {
			// 	uni.setStorageSync('coinList', res.data)
			// })
			this.checkTimeout()
			this.setupTimer()
		},
		onHide: function() {
			console.log('App Hide')
			this.clearTimer()
			uni.setStorageSync('time', this.time)
		}
	}
</script>

<style>
	@import '@/common/uni.css';
	@import '@/common/custom.css';
	@import '@/common/uni-icons.css';

	.navbar-left {
		height: 100%;
		display: flex;
		align-items: center;
	}

	.logo {
		width: 215px;
		background-color: #3B993B;
		height: 100%;
		padding-left: 24px;
		display: flex;
		align-items: center;
		position: relative;
		left: -20px;
		border-bottom: 1px solid rgba(255, 255, 255, 0.5);
	}

	.header {
		border-bottom: 0 !important;
	}

	.text-green {
		color: #12A234;
	}

	.text-white {
		color: #FFFFFF;
	}

	.text-normal {
		font-size: 6.25rpx;
	}

	.page-container {
		box-shadow: 0px 2px 40px rgba(0, 0, 0, 0.1);
		border-radius: 12px;
	}
	
	.el-table th > .cell {
		word-break: keep-all !important;
	}
</style>
